package com.beiing.tianshuai.tianshuai.message.model;

import com.beiing.tianshuai.tianshuai.base.BasePresenter;
import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.GroupDetailsBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Jinni on 2017/9/21 0021.
 */

public class GroupDetailsModel extends BasePresenter implements GroupDetailsModelImpl {

    private static final String TAG = "GroupDetailsModel";
    private OnRequestListener mListener;

    public GroupDetailsModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getGroupDetailsRequestResponse(String uid, String gid) {
        Observable<GroupDetailsBean> response =
                DataManager.getGroupDetailsRequestResponse(uid, gid);
        Observable<GroupDetailsBean> observable = response.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new DisposableObserver<GroupDetailsBean>() {
            @Override
            public void onNext(@NonNull GroupDetailsBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("===================== onComplete =======================", TAG);
            }
        });
    }

    @Override
    public void getDismissGroupResult(String uid, String id, String groupId) {
        Observable<CodeBean> response =
                DataManager.getDismissGroupResult(uid, id, groupId);
        Observable<CodeBean> observable = response.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (mListener != null) {
                    mListener.onDismissSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("===================== onComplete =======================", TAG);
            }
        });
    }

    @Override
    public void getQuitGroupResult(String uid, String id, String groupId) {
        Observable<CodeBean> response =
                DataManager.getQuitGroupResult(uid, id, groupId);
        Observable<CodeBean> observable = response.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (mListener != null) {
                    mListener.onQuitSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("===================== onComplete =======================", TAG);
            }
        });
    }

    @Override
    public void getEditInvitedRootResult(String uid, String gid) {
        Observable<CodeBean> response =
                DataManager.getEditInvitedRootResult(uid, gid);
        Observable<CodeBean> observable = response.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (bean.getCode() == 200) {
                    LogUtils.i("============= 允许群成员邀请好友权限修改成功 =============", TAG);
                } else {
                    LogUtils.e("============= 允许群成员邀请好友权限修改失败 =============", TAG);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("===================== onComplete =======================", TAG);
            }
        });
    }

    @Override
    public void getUpdateAvatarResult(String uid, String id, File updateAvatar) {
        //构建body
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("uid", uid)
                .addFormDataPart("gid", id)
                .addFormDataPart("photo", updateAvatar.getName(), RequestBody.create(MediaType.parse("image/*"), updateAvatar))
                .build();

        Observable<CodeBean> loginResult = DataManager.getUpdateGroupAvatarResult(
                requestBody
        );
        Observable<CodeBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CodeBean>() {

            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (mListener != null) {
                    mListener.updateAvatarSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {
        void onSuccess(GroupDetailsBean bean);
        void onDismissSuccess(CodeBean bean);
        void onQuitSuccess(CodeBean bean);
        void updateAvatarSuccess(CodeBean bean);
        void onError(Throwable throwable);
    }
}
